import { App, DirectiveBinding } from 'vue';
import common from "../common/common";
import {RoleMenuItem} from "../types/common";

const hasPermission = (code: string) => {
    const localRoleMenuList = common.getLocalRoleAuth() || []
    return localRoleMenuList.findIndex((roleMenu: RoleMenuItem) => roleMenu.menuCode === code) > -1
}
const isAuthDirective = {
    mounted(el: HTMLElement, binding: DirectiveBinding<string[]>) {
        const btnCode = binding.value as string;
        const isAuth = hasPermission(btnCode);
        if (!isAuth) {
            el.parentNode?.removeChild(el);
        }
    }
};
// 这里会默认导出一个 isAuthDirective方法
export default {
    install(app: App, options: any) {
        app.directive('authOperate', isAuthDirective);
    }
};
